/*
	视频详情
		视频修改
		视频删除
*/
var adminLoginedInfo = Cookies.getJSON('kplus_ad_logined_info');

//dom
$(function(){
	layui.use(['layer', 'form'], function(){
		var layer = layui.layer;
		var form = layui.form;

		var ajaxData = {
			aId: adminLoginedInfo.aId,
			aToken: adminLoginedInfo.aToken
		};

		// wang editor 富文本编辑器
		var WEditor = window.wangEditor,
            editorCn = new WEditor('#htmlCn'),
            editorTw = new WEditor('#htmlTw'),
            editorEn = new WEditor('#htmlEn');
        editorCn.create();
        editorTw.create();
        editorEn.create();

        //初始化表单
        function initForm(responseData){
        	var data = responseData.data[0];
        	var form_init_data = {
        		titleCn: data.title['cn'],
        		titleTw: data.title['tw'],
        		titleEn: data.title['en'],
        		videoImg: data.video_img,
        		status: data.status,
        		videoCn: data.link['cn'],
        		videoTw: data.link['tw'],
        		videoEn: data.link['en']
        	};
        	//富文本初始化赋值
        	editorCn.txt.html(data.html_cn);
            editorTw.txt.html(data.html_tw);
            editorEn.txt.html(data.html_en);
            //表单初始化赋值
            form.val('video-detail-form', form_init_data);
        }
		//拉取视频
		function getVideo(){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] }); 
			var _ajaxData = $.extend({vId: getParam('v_id')}, ajaxData);
			$.ajax({
				url: BASE_SERVICEURL + URlS.video,
				type: 'GET',
				data: _ajaxData,
				success: function(responseData){
					//console.log(responseData)
					layer.close(loadIndex);
					if(responseData.msg.code == 200){
						initForm(responseData);
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					}
				},
				error: function(responseData){
					layer.close(loadIndex);
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						location.href = './500.html';
					}})
				}
			})
		};
		function modifyVideoInfo(newData){
			var loadIndex = layer.load(1, { shade: [0.5, '#000'] }); 
			var _ajaxData = $.extend({vId: getParam('v_id')}, newData, ajaxData);

			$.ajax({
				url: BASE_SERVICEURL + URlS.video,
				type: 'POST',
				data: _ajaxData,
				success: function(responseData){
					if(responseData.msg.code == 200){
						window.location.reload();
					}else{
						layer.msg(responseData.msg.msg, {time: 2000});
					}
					layer.close(loadIndex);
				},
				error: function(responseData){
					layer.msg(MESSAGES.service_error, {time: 2000, end: function(){
						location.href = './500.html';
					}})
					layer.close(loadIndex);
				}
			})

		}
		// 修改信息
		form.on('submit(modify)', function(data){
			layer.confirm(MESSAGES.confirm_modify, {title: '提示'}, function(index){
				var f = data.field;
				modifyVideoInfo({
					title: langTransObj(f.titleCn, f.titleTw, f.titleEn),
        			status: f.status,
        			link: langTransObj(f.videoCn, f.videoTw, f.videoEn),
        			img: f.videoImg,
        			cn: editorCn.txt.html(),
                	tw: editorTw.txt.html(),
                	en: editorEn.txt.html()
				})
				layer.close(index);
			});
			return false;
		})
		//删除视频
		form.on('submit(delete)', function(data){
			
		})



		getVideo();


	});
});


